import { v4 } from 'uuid'
export default {
    beforecreate() {
        if (!this.sockets) {
            this.sockets = {}
        }

        this.sockets.subscribe = (event, callback) => {
            this.$imSocketIo.addListener(event, callback, this)
        }

        this.sockets.unsubscribe = (event) => {
            this.$imSocketIo.removeListener(event, this)
        }
    },
    mounted() {
        if (this.$options.sockets) {
            Object.keys(this.$options.sockets).forEach((event) => {
                if (event != 'subscribe' && event != 'unsubscribe') {
                    this.$imSocketIo.emitter.addListener(event, this.$options.sockets[event], this)
                }
            })
        }
    },
    methods: {
        $startCall(caller_user_id, target_user_id, current_user_id, mediaType) {
            this.$imSocketIo.startCall(caller_user_id, target_user_id, current_user_id, mediaType)
        },
        $connectImSocket({ user_id, token }) {
            let resource = v4()
            this.$socket.emit('imReconnect', { user_id, token, resource })
        },
        $sendMessage(message) {
            this.$socket.emit('imWebMsg', JSON.stringify(message))
        },
        $getHistoryMessage({ user_id, target_id, page, is_support = false }) {
            return this.$imSocketIo.getHistory({ userId: user_id, targetId: target_id, page, isSupport: is_support })
        }
    }
}